package com.ym.b2b.user.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ym.b2b.commons.conf.B2BConstants;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.user.service.UserFavoritesService;
import com.ym.b2b.user.sqlbean.EmSUserFavorites;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/trade")
public class UserFavoritesController extends BaseController {

    private Logger               logger = Logger.getLogger(getClass());

    @Autowired
    private UserFavoritesService userFavoritesService;

    @RequestMapping("searchUserProductFavorites.s")
    public ModelAndView searchUserProductFavorites(EmSUserFavorites emSUserFavorites, Page<EmSUserFavorites> page,
                                                   HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);
            if (token == null) {
                m.setViewName("website/login");
                return m;
            }
            emSUserFavorites.setUserId(Long.parseLong(String.valueOf(token.getUserId())));
            page.setPageable("true");
            page.setPageUrl(req);
            result.setResultPage(userFavoritesService.searchPageUserProductFavorites(emSUserFavorites, page));
            result.putData("svo", emSUserFavorites);
            m.addObject("result", result);
            m.setViewName("center/user_product_favorites");
            logger.info("url");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            m.setViewName(B2BConstants.WEB_CENTER_ERROR);
        }
        return m;
    }

    @RequestMapping("searchPageUserProductFavorites.s")
    @ResponseBody
    public Result searchPageUserProductFavorites(EmSUserFavorites emSUserFavorites, Page<EmSUserFavorites> page,
                                                 HttpServletRequest req) {
        Result result = new Result();
        try {
            WebTokenUser token = getWebTokenUser(req);

            emSUserFavorites.setUserId(Long.parseLong(String.valueOf(token.getUserId())));

            result.setResultPage(userFavoritesService.searchPageUserProductFavorites(emSUserFavorites, page));

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("deleteUserFavorites.s")
    @ResponseBody
    public Result deleteUserFavorites(Long favoritesId) {
        Result ret = new Result();
        if (null == favoritesId) {
            ret.setCodeResult(Result.CODE_100, "必填参数不能为空");
            return ret;
        }
        logger.info(favoritesId);
        ret = userFavoritesService.deleteUserFavorites(favoritesId);
        return ret;
    }

    @RequestMapping("addUserFavorites.s")
    @ResponseBody
    public Result addUserFavorites(EmSUserFavorites emSUserFavorites, HttpServletRequest req) {

        Result ret = new Result();
        WebTokenUser tk = getWebTokenUser(req);
        if (null == tk) {
            ret.setCodeResult(Result.CODE_100, "请先登录");
            return ret;
        }
        emSUserFavorites.setUserId(tk.getUserId());
        try {
            emSUserFavorites.setCreateDate(new Date());
            ret = userFavoritesService.addUserFavorites(emSUserFavorites);
        } catch (Exception e) {
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }
}
